package com.resou.reader.mine.IView;

import android.text.TextUtils;

import com.resou.reader.ReSouApplication;
import com.resou.reader.api.entry.LoginData;
import com.resou.reader.api.entry.UserInfoBean;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.log.DLog;

/**
 * Created by Garrett on 2018/8/29.
 * contact me krouky@outlook.com
 * 用户实例为单例
 */
public class UserInstance {

    private static UserInstance sUser;

    private UserInstance() {
    }

    private static String sToken;
    private static LoginData sLoginData;

    public static boolean isLogin() {
        return !TextUtils.isEmpty(getToken());
    }

    public static String getToken() {
        if (TextUtils.isEmpty(sToken)) {
            sToken = PreferenceUtil.getInstance(ReSouApplication.getContext()).loadUserToken();
        }
        return sToken;
    }

    public static void setToken(String token) {
        sToken = token;
        PreferenceUtil.getInstance(ReSouApplication.getContext()).saveUserToken(token);
    }

    /**
     * 判断是否是游客登录
     */
    public static boolean isTourist() {
        LoginData loginData = PreferenceUtil.getInstance(ReSouApplication.getContext()).loadLoginData();
        String usertype = "100";
        if (loginData != null) {
            usertype = loginData.getUserType() + "";
        }
        if ("100".equals(usertype)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 用户id
     *
     * @return
     */
    public static String getUserId() {
        if (sLoginData == null) {
            sLoginData = PreferenceUtil.getInstance(ReSouApplication.getContext()).loadLoginData();
            if (sLoginData == null) {
                sLoginData = new LoginData();
            }
        }
        return sLoginData.getId() + "";
    }

    public static LoginData getLoginData() {
        if (sLoginData == null) {
            sLoginData = PreferenceUtil.getInstance(ReSouApplication.getContext()).loadLoginData();
        }
        if (sLoginData == null) {
            sLoginData = new LoginData();
        }
        return sLoginData;
    }

    public static void setLoginData(LoginData loginData) {
        sLoginData = loginData;
        PreferenceUtil.getInstance(ReSouApplication.getContext()).saveLoginData(loginData);
    }

    public static LoginData saveUserInfo(UserInfoBean.UBean uBean) {
        if (uBean == null) {
            return sLoginData;
        }
        LoginData data = new LoginData();
        data.setPhone(uBean.getPhone());
        data.setBlance(uBean.getBlance());
        data.setGiftBlance(uBean.getGiftBlance());
        data.setBirthday(uBean.getBirthday());
        data.setGender(String.valueOf(uBean.getGender()));
        data.setNickName(uBean.getNickName());
        data.setHead_pic(uBean.getHead_pic());
        data.setId(uBean.getId());
        data.setUserLevel(uBean.getVipLevel());
        data.setUserType(uBean.getUserType());
        data.setEnergy(uBean.getEnergy());
        data.setIsVipUser(uBean.isVipUser());
        data.setVipEndTime(uBean.getVipEndTime());
        data.setReadType(uBean.getReadType());
        setLoginData(data);
        return sLoginData;
    }


}
